//
//  GYMineViewController.m
//  research
//
//  Created by 章鱼 on 17/3/27.
//  Copyright © 2017年 simon. All rights reserved.
//

#import "GYMineViewController.h"
#import "GYUserNoLoginTopView.h"
#import "GYUserCell.h"
#import "GYUserLoginTopView.h"
#import "GYConfigDBManager.h"
#import "GYLoginViewController.h"
#import "GYRegisterViewController.h"
#import "GYUserInfoViewController.h"
#import "GYModifyViewController.h"
#import "GYMyCollectViewController.h"
#import "GYSettingViewController.h"

@interface GYMineViewController ()<
UITableViewDataSource,
UITableViewDelegate
>

@property (nonatomic, strong)GYUserNoLoginTopView *nologinView;
@property (nonatomic, strong)GYUserLoginTopView *loginView;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, assign)BOOL isLogin;

@end

@implementation GYMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self basicSetup];
    [self initTopView];
    [self tableview];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if ([[GYConfigDBManager defaultManager] isLogin]) {
        _loginView.nameLable.text = [[GYConfigDBManager defaultManager] userInformation].nickname;
        _loginView.locationLable.text = [[GYConfigDBManager defaultManager] userInformation].address;
        _isLogin = YES;
    }else {
        _isLogin = NO;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)basicSetup {
    [self initNavBarWithTitle:@"个人中心" tintColor:[UIColor orz_mainColor]];
    [self.navBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                         [UIColor whiteColor],NSForegroundColorAttributeName,
                                         [UIFont boldSystemFontOfSize:18],
                                         NSFontAttributeName,
                                         nil]];
    [self.view setBackgroundColor:[UIColor orz_globalBackground]];


}

- (void)initTopView {
    _isLogin = [[GYConfigDBManager defaultManager] isLogin];
    if (_isLogin) {
        _loginView = [[GYUserLoginTopView alloc] init];
        _loginView.nameLable.text = [[GYConfigDBManager defaultManager] userInformation].nickname;
        _loginView.locationLable.text = [[GYConfigDBManager defaultManager] userInformation].address;
        [self.view addSubview:_loginView];
        [_loginView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(64.f);
            make.left.mas_equalTo(0.f);
            make.right.mas_equalTo(0.f);
            make.height.mas_equalTo(155.f*ADJUST_SCREEN_HEIGHT_SCALE);
            
        }];
    }else {
        _nologinView = [[GYUserNoLoginTopView alloc] init];
        [self.view addSubview:_nologinView];
        WS(weakSelf);
        _nologinView.resigerBlock = ^ void() {
            SS(strongSelf);
            [strongSelf startResigerUser];
        };
        
        _nologinView.loginBlock = ^(){
            SS(strongSelf);
            [strongSelf startLoginView];
        };
        [_nologinView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(64.f);
            make.left.mas_equalTo(0.f);
            make.right.mas_equalTo(0.f);
            make.height.mas_equalTo(155.f*ADJUST_SCREEN_HEIGHT_SCALE);
            
        }];
    }

}

#pragma mark - UI
- (UITableView *)tableview {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _tableView.backgroundColor = [UIColor orz_globalBackground];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = NO;
        _tableView.estimatedRowHeight=40.0f;
        _tableView.rowHeight=UITableViewAutomaticDimension;
        _tableView.layer.borderColor = [UIColor orz_line].CGColor;
        _tableView.layer.borderWidth = 1.f;
        [self.view addSubview:_tableView];
        [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        [self.tableView registerClass:[GYUserCell class] forCellReuseIdentifier:@"usercell"];
        [self initUserViewUI];
        
    }
    return _tableView;
}


- (void)initUserViewUI {
    if (_isLogin) {
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_loginView.mas_bottom).offset(10.f);
            make.right.mas_equalTo(self.view);
            make.left.mas_equalTo(self.view);
            make.bottom.mas_equalTo(self.view);
        }];
    }else {
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_nologinView.mas_bottom).offset(10.f);
            make.right.mas_equalTo(self.view);
            make.left.mas_equalTo(self.view);
            make.bottom.mas_equalTo(self.view);
        }];
    }
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GYUserCell *cell = [tableView dequeueReusableCellWithIdentifier:@"usercell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.titleImage.image = [UIImage imageNamed:@"personal center_icon_-details"];
            cell.infoLable.text = @"个人信息";
            
        }
        if (indexPath.row == 1) {
            cell.titleImage.image = [UIImage imageNamed:@"personal center_icon_pass"];
            cell.infoLable.text = @"修改密码";
            cell.lineView.hidden = YES;
        }
        
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.titleImage.image = [UIImage imageNamed:@"personal center_icon_collection"];
            cell.infoLable.text = @"已答问卷";
        }
        if (indexPath.row == 1) {
            cell.titleImage.image = [UIImage imageNamed:@"personal center_icon_Settings"];
            cell.infoLable.text = @"设置";
            cell.lineView.hidden = YES;

        }
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return 0;
    }else{
        return 20;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

- (void)startResigerUser {

    GYRegisterViewController *registerCtrl = [[GYRegisterViewController alloc] init];
    registerCtrl.hidesBottomBarWhenPushed = YES;
    registerCtrl.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:registerCtrl animated:YES];
}

- (void)startLoginView {
    GYLoginViewController *loginCtrl = [[GYLoginViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginCtrl];
    [self presentViewController:nav animated:YES completion:NULL];
}

#pragma mark - tableViewcell点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [[YGHttpManager defaultManager] fetchGetUserIsloginWithtoken:[[GYConfigDBManager defaultManager] gyToken] success:^(id responseObj) {
        //重新登录
        if ([[responseObj objectForKey:@"status"] integerValue] == 0) {
            if ([[GYConfigDBManager defaultManager] userInformation].userName && [[GYConfigDBManager defaultManager] userInformation].password) {
                
                [[YGHttpManager defaultManager] fetchGetUserLoginWithuserName:[[GYConfigDBManager defaultManager] userInformation].userName password:[[GYConfigDBManager defaultManager] userInformation].password success:^(id responseObj) {
                    GYCommonModel *commonmodel = [GYCommonModel mj_objectWithKeyValues:responseObj];
                    if ([[responseObj objectForKey:@"status"] integerValue] == 0) {
                        [VBAlertView showTextHUD:commonmodel.message];
                        return ;
                    }
                    [[GYConfigDBManager defaultManager] setLogin:YES];
                    [[GYConfigDBManager defaultManager] setGYToken:[commonmodel.data objectForKey:@"token"]];
                    GYUserInfoModel *model = [GYUserInfoModel mj_objectWithKeyValues:[commonmodel.data objectForKey:@"user"]];
                    [[GYConfigDBManager defaultManager] setUserInformation:model];
                    
                } failure:^(NSError *error) {
                    
                }];
            }else {
                
                GYLoginViewController *loginCtrl = [[GYLoginViewController alloc] init];
                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginCtrl];
                [self presentViewController:nav animated:YES completion:NULL];
                
                return ;
            }
        }
        
        if (![[GYConfigDBManager defaultManager] isLogin]) {
            GYLoginViewController *loginCtrl = [[GYLoginViewController alloc] init];
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginCtrl];
            [self presentViewController:nav animated:YES completion:NULL];
            
        }
        if (indexPath.section == 0) {
            
            if (indexPath.row == 0) {
                GYUserInfoViewController *userCtrl = [[GYUserInfoViewController alloc] init];
                userCtrl.hidesBottomBarWhenPushed = YES;
                userCtrl.tabBarController.tabBar.hidden = YES;
                [self.navigationController pushViewController:userCtrl animated:YES];
                
            }
            if(indexPath.row == 1) {
                GYModifyViewController *modifyCtrl = [[GYModifyViewController alloc] init];
                modifyCtrl.hidesBottomBarWhenPushed = YES;
                modifyCtrl.tabBarController.tabBar.hidden = YES;
                [self.navigationController pushViewController:modifyCtrl animated:YES];
                
            }
        }
        if (indexPath.section == 1) {
            
            if (indexPath.row == 0) {
                GYMyCollectViewController *collectCtrl = [[GYMyCollectViewController alloc] init];
                collectCtrl.hidesBottomBarWhenPushed = YES;
                collectCtrl.tabBarController.tabBar.hidden = YES;
                [self.navigationController pushViewController:collectCtrl animated:YES];
                
            }
            if(indexPath.row == 1) {
                GYSettingViewController *settingCtrl = [[GYSettingViewController alloc] init];
                settingCtrl.hidesBottomBarWhenPushed = YES;
                settingCtrl.tabBarController.tabBar.hidden = YES;
                [self.navigationController pushViewController:settingCtrl animated:YES];
                
            }
        }

        
        
    } failure:^(NSError *error) {
        
    }];
}
@end
